﻿using System;
using System.Linq;
using Ones.ServiceCategoryManagement;
using Ones.ServiceHumanResourceManagement;
using ns = Ones.ServiceCategoryManagement;
using ns1 = Ones.ServiceHumanResourceManagement;

namespace Ones.WebAdmin
{
    public partial class CatMgmtCatPage : System.Web.UI.Page
    {
        private ServiceCategoryManagementClient c = null;
        private ServiceHumanResourceManagementClient d = null;

        private ns.Category cat = null;
        private ns1.Division div = null;
        private string itemId = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                loadParent();
                loadDivision();

                if (Session["cmd"] != null)
                {
                    fillData();

                    if (Session["cmd"].ToString().Equals("delete"))
                    {
                        txtName.Enabled = false;
                        txtDescr.Enabled = false;
                        cbbParentCate.Enabled = false;
                        cbbDivision.Enabled = false;
                    }
                }
            }
        }

        public void chkNoParent_CheckedChanged(object sender, EventArgs e)
        {
            if (chkNoParent.Checked)
            {
                cbbParentCate.Enabled = false;
                cbbDivision.Enabled = true;
            }
            else
            {
                cbbParentCate.Enabled = true;
                cbbDivision.Enabled = false;
            }
        }

        public void btnAdd_Click(object sender, EventArgs e)
        {
            c = new ServiceCategoryManagementClient("catWsHttpBinding");
            cat = new ns.Category();

            cat.catName = txtName.Text;
            cat.catDescription = txtDescr.Text;

            cat.catParentId = null;
            cat.belongToDivision = cbbDivision.SelectedValue;

            if (chkNoParent.Checked)
            {
                cat.catParentId = cbbParentCate.SelectedValue;
                cat.belongToDivision = null;
            }

            if (c.addCategory(cat))
            {
                Response.Redirect("~/WebAdmin/CatListCatPage.aspx");
            }
        }

        public void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceCategoryManagementClient("catWsHttpBinding");
                cat = new ns.Category();

                cat.catId = Session["id"].ToString();
                cat.catName = txtName.Text;
                cat.catDescription = txtDescr.Text;
                cat.catParentId = cbbParentCate.SelectedValue;
                cat.belongToDivision = cbbDivision.SelectedValue;

                if (c.updateCategory(cat))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/WebAdmin/CatListCatPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnCancel_Click(object sender, EventArgs e)
        {
            Session.Remove("cmd");
            Response.Redirect("~/WebAdmin/CatListCatPage.aspx");
        }

        private void loadParent()
        {
            c = new ServiceCategoryManagementClient("catBasicHttpBinding");
            var lst = c.searchAllCategories();

            cbbParentCate.DataSource = lst;
            cbbParentCate.DataValueField = "catId";
            cbbParentCate.DataTextField = "catName";
            cbbParentCate.DataBind();

            c.Close();
        }

        private void loadDivision()
        {
            d = new ServiceHumanResourceManagementClient("hrBasicHttpBinding");
            var lst = d.searchAllDivisions();

            cbbDivision.DataSource = lst;
            cbbDivision.DataValueField = "divId";
            cbbDivision.DataTextField = "divName";
            cbbDivision.DataBind();

            d.Close();
        }

        private void fillData()
        {
            c = new ServiceCategoryManagementClient("catBasicHttpBinding");

            itemId = Session["id"].ToString();
            cat = c.searchCategoryById(itemId);

            txtName.Text = cat.catName;
            txtDescr.Text = cat.catDescription;
            cbbParentCate.SelectedValue = cat.catParentId;
            cbbDivision.SelectedValue = cat.belongToDivision;

            c.Close();
        }
    }
}